package tlias.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tlias.service.DeptService;
import tlias.annotation.Crud;
import tlias.pojo.Dept;
import tlias.pojo.Result;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result list(){
        return Result.success(deptService.getAllDepts());
    }
    @DeleteMapping
    @Crud
    public Result delete(@RequestParam("id") Integer id){
        log.info("要删除的部门id是：{}",id);
        deptService.delete(id);
        return Result.success();
    }
    @PostMapping
    @Crud
    public Result add(@RequestBody Dept dept){
        log.info("要添加的部门是：{}",dept.getName());
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("要查询的部门id是：{}",id);

        return Result.success(deptService.getById(id));
    }
    @PutMapping
    @Crud
    public Result update(@RequestBody Dept dept){
        log.info("要修改的部门是：{}",dept.getName());
        deptService.modify(dept);
        return Result.success();
    }
}
